<template>
    <div class="detail">
        <div class="top">
            <div class="book-img">
                <img v-if="goods.pic" :src="baseURL + goods.pic" alt="">
            </div>
            <div class="book-detail">
                <div class="book-title">
                    <h1>{{ goods.title }} <el-tag style="margin-left: 10px;">{{category[goods.category_id]}}</el-tag>
                    </h1>
                </div>
                <div class="info">
                    <div class="book-view">
                        <el-icon>
                            <View />
                        </el-icon>
                        {{ goods.view }}
                    </div>
                    <div>
                        <el-icon>
                            <Coin />
                        </el-icon>
                        {{ goods.count }}库存
                    </div>
                    <div>
                        ￥{{ formatPrice(goods.price) }}
                    </div>
                </div>
                <div class="book-desc">
                    <p><b>简介：</b>{{ goods.descrption }}</p>
                </div>
                <div class="btns">
                    <el-button type="warning" @click="addCollect">
                        <el-icon size="20">
                            <Star />
                        </el-icon>
                        收藏
                    </el-button>
                    <el-button type="primary" @click="addList">
                        <el-icon size="20">
                            <ShoppingCart />
                        </el-icon>
                        &nbsp;加入购物车
                    </el-button>
                </div>
            </div>
        </div>
        <Comment :id="route.params.id"></Comment>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRoute } from 'vue-router'
import { bookDetail, viewAdd, getCategory } from '@/api/book'
import { create } from '@/api/collect'
import { create as borrowCreate } from '@/api/borrow'
import { baseURL } from '@/config/baseURL'
import Comment from '@/components/Comment.vue'
import { View, ShoppingCart, Star, Coin } from '@element-plus/icons-vue'
import { formatPrice } from '@/utils/formatPrice'
import { useUserStore } from '@/stores/useUser'
import moment from 'moment'
import { ElMessage } from 'element-plus'

const userStore = useUserStore()
const route = useRoute()

const goods = ref({
    id: '',
    title: '',
    author: '',
    descrption: '',
    pic: '',
    view: '',
})

//分类列表
const category = ref({})

const getCategoryList = () => {
    getCategory({}).then(res => {
        if (res.data.code === 200) {
            category.value = res.data.categoryList;
        } else {
            ElMessage.error(res.data.message)
        }
    })
}
//获取分类
getCategoryList()

if (route.params.id) {

    bookDetail({ id: route.params.id }).then(res => {
        if (res.data.code == 200) {
            goods.value = { ...res.data.goods }
        }
    })
}

//浏览量增长
viewAdd({ id: route.params.id }).then(res => {
    if (res.data.code == 200) {
        goods.value.view += 1
    }
})

//加入购物车
const addList = () => {

    if (!userStore.isLogin) {
        ElMessage.error('请先登录')
        return
    }

    borrowCreate({
        user_id: userStore.userinfo.id,
        goods_id: goods.value.id,
        day: 1,
        status: 0,
        create_time: moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
    }).then(res => {
        if (res.data.code == 200) {
            ElMessage.success(res.data.message)
        }
        else if (res.data.code == 300) {
            ElMessage.warning(res.data.message)
        } else {
            ElMessage.error(res.data.message)
        }
    })
}

//收藏
const addCollect = () => {

    if (!userStore.isLogin) {
        ElMessage.error('请先登录')
        return
    }

    create({
        goods_id: route.params.id,
        user_id: userStore.userinfo.id,
        create_time: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
        goods: goods.value.title
    }).then(res => {
        if (res.data.code == 200) {
            ElMessage.success(res.data.message)
        }
        else if (res.data.code == 300) {
            ElMessage.warning(res.data.message)
        } else {
            ElMessage.error(res.data.message)
        }
    })
}


</script>

<style scoped lang='less'>
.detail {
    width: 100%;
    min-height: calc(100vh - 150px);
    background-color: #fff;
    margin-top: 30px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 20px;
    box-sizing: border-box;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);

    .top {
        width: 100%;
        border-radius: 10px;
        display: flex;

        .book-img {
            border-radius: 10px;
            overflow: hidden;

            img {
                width: 250px;
                height: 250px;
                object-fit: cover;
            }
        }

        .book-detail {
            flex: 1;
            position: relative;
            margin-left: 20px;

            .book-title {
                display: flex;
                justify-content: space-between;
                align-items: center;

                h1 {
                    font-size: 20px;
                    color: #383838;
                }

                h2 {
                    font-size: 16px;
                    color: #777;
                }
            }

            .book-desc {
                margin-top: 20px;
                font-size: 16px;
                color: #777;
                line-height: 28px;
            }

            .info {
                margin-top: 20px;
                display: flex;
                color: #777;

                &>div {
                    margin-right: 30px;
                    display: flex;
                    align-items: center;

                    i {
                        margin-right: 5px;
                    }
                }
            }

            .btns {
                width: 100%;
                position: absolute;
                bottom: 0;
                display: flex;
                justify-content: end;
            }
        }
    }
}
</style>